package com.ly.hotel.spainterface.jg.view;

import android.app.Dialog;
import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.spainterface.jg.ActivityCollector;

/**
 * Created by ThinkPad on 2018/2/7.
 */

public class CustomErrorDialogFragment extends DialogFragment {

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        Window window = dialog.getWindow();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.5), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.windowAnimations = R.style.customDialogAnim;
        window.setAttributes(layoutParams);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 设置背景半透明
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置无标题
        View view = inflater.inflate(R.layout.jg_custom_error_dialog,container,false);
        Bundle bundle = getArguments();
        ImageButton errorClose = (ImageButton) view.findViewById(R.id.error_tips_close);
        TextView errorTips = (TextView) view.findViewById(R.id.error_tips);
        Button errorConfirm = (Button) view.findViewById(R.id.error_confirm);
        errorClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();// 关闭错误提示页面
            }
        });
        if(bundle != null){
            String errorMsg = bundle.getString("errorMsg");
            errorTips.setText(errorMsg);
        }
        errorConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityCollector.finishAll();
            }
        });
        return view;
    }
}
